/*
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.mrd.gtimpframe.action;

import com.alibaba.fastjson.JSONObject;
import com.mrd.base.Jqgrid;
import com.mrd.framework.config.web.json.Result;
import com.mrd.gtimpframe.entity.GenProtocolUnite;
import com.mrd.gtimpframe.service.ProtocolConfirmService;
import com.mrd.gtimpframe.service.ProtocolService;
import com.mrd.util.LoginUtil;
import com.mrd.util.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

import javax.annotation.Resource;

/**
 * Created by dengdc on 2017/7/31.
 */
@RequestMapping("/fund/protocol")
@Controller
public class ProtocolAction {
	private Logger					logger	= LoggerFactory.getLogger(ProtocolAction.class);
	@Resource
	private ProtocolService			protocolService;
	@Resource
	private ProtocolConfirmService	protocolConfirmService;

	/**
	 * 协议、报价管理页面
	 */
	@RequestMapping("/manage/unite/view")
	public String uniteManageView() {
		return "/gtimpframe/protocol/manageViewUnite";
	}
	/**
	 * 管理页面列表数据
	 * @param jqgrid
	 * @return
	 */
	@RequestMapping("/manage/unite/viewData")
	@ResponseBody
	public Page uniteManageViewData(Jqgrid jqgrid) {
		return protocolService.uniteManageViewData(jqgrid);
	}
	/**
	 * 添加、修改协议和报价页面
	 */
	@RequestMapping("/manage/unite/addView")
	public String uniteAddView() {
		return "/gtimpframe/protocol/addProtocolUnite";
	}
	/**
	 * 添加、修改协议和报价页面
	 */
	@RequestMapping("/manage/unite/editView")
	public String uniteEditView() {
		return "/gtimpframe/protocol/editProtocolUnite";
	}

	/**
	 * 添加、修改协议和报价页面
	 */
	@RequestMapping("/manage/unite/readView")
	public String onlyreadView(Model model) {
		model.addAttribute("readonly" ,"1");
		return "/gtimpframe/protocol/editProtocolUnite";
	}

	/**
	 * 保存报价
	 * @return
	 */
	@RequestMapping("/manage/unite/saveOrUpdate")
	@ResponseBody
	public Result saveOrUpdateUnite(GenProtocolUnite unite, MultipartFile attachment) {
		try {
			return protocolService.saveOrUpdateProtocolUnite(unite, attachment);
		}catch (Exception e){
			e.printStackTrace();
		}
		return Result.newFailInstance("本次保存无效，请稍后再试");
	}

	/**
	 * 查询数据用以编辑
	 * @param id
	 *            报价协议ID
	 */
	@RequestMapping("/manage/unite/editData")
	@ResponseBody
	public JSONObject findUniteForEdit(String id) {
		return protocolService.findUniteForEdit(id);
	}

	/**
	 * 协议、报价管理页面--协议明细
	 */
	@RequestMapping("/manage/view")
	public String manageView() {
		return "/gtimpframe/protocol/manageView";
	}

	/**
	 * 添加、修改协议和报价页面--协议明细
	 */
	@RequestMapping("/manage/editView")
	public String editView() {
		return "/gtimpframe/protocol/editProtocol";
	}

	/**
	 * 保存报价
	 * @return
	 */
	@RequestMapping("/manage/saveOrUpdate")
	@ResponseBody
	public Result saveOrUpdate() {
		return protocolService.saveOrUpdateProtocolFromEditPage();
	}

	/**
	 * 管理页面列表数据
	 * @param jqgrid
	 * @return
	 */
	@RequestMapping("/manage/viewData")
	@ResponseBody
	public Page manageViewData(Jqgrid jqgrid, String uniteId) {
		return protocolService.manageViewData(jqgrid, uniteId);
	}

	/**
	 * 查询数据用以编辑--协议明细
	 * @param id
	 *            报价协议ID
	 */
	@RequestMapping("/manage/editData")
	@ResponseBody
	public Map<String, Object> findProtocolForEdit(String id) {
		if(StringUtils.isEmpty(id)){
			return null;
		}
		return protocolService.findForEditPage(id);
	}

	/**
	 * 发送报价协议至供应商
	 * @param uniteId
	 *            报价协议ID
	 */
	@RequestMapping("/biz/sendUniteToSup")
	@ResponseBody
	public Result sendUniteToSup(String uniteId) {
		try {
			return protocolService.sendUniteToSupplier(uniteId);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("发送报价协议至供应商时出现了异常: " + e.getMessage());
			return Result.newFailInstance("出现了未知的错误");
		}
	}

	/**
	 * 发送报价协议至供应商
	 * @param uniteId
	 *            报价协议ID
	 */
	@RequestMapping("/biz/cancelSendUniteFromSup")
	@ResponseBody
	public Result cancelSendUniteFromSup(String uniteId) {
		try {
			return protocolService.cancelSendUniteToSupplier(uniteId);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("发送报价协议至供应商时出现了异常: " + e.getMessage());
			return Result.newFailInstance("出现了未知的错误");
		}
	}

	/**
	 * 前往供应商确认报价页面
	 */
	@RequestMapping("/biz/toSupConfirm")
	public String toSupConfirm() {
		return "/gtimpframe/protocol/supConfirmManageView";
	}

	/**
	 * 前往供应商管理确认报价页面
	 */
	@RequestMapping("/biz/supConfirmManageView")
	public String supConfirmManageView() {
		return "/gtimpframe/protocol/supConfirmManageView";
	}

	/**
	 * 报关行确认报价管理页面数据
	 * @param jqgrid
	 * @return
	 */
	@RequestMapping("/biz/supConfirmGridData")
	@ResponseBody
	public Page supConfirmGridData(Jqgrid jqgrid) {
		return protocolService.uniteSupViewData(jqgrid);
	}

	/**
	 * 前往供应商确认报价页面
	 * @param confirmId
	 * @return
	 */
	@RequestMapping("/biz/getSupConfirmData")
	@ResponseBody
	public Result getSupConfirmData(String confirmId) {
		return protocolConfirmService
		        .getConfirmPageData(confirmId, LoginUtil.getLoginUser().getCompany().getId());
	}

	/**
	 * 供应商进行确认报价
	 * @param confirmId
	 *            确认书ID
	 */
	@RequestMapping("/biz/supDidConfirm")
	@ResponseBody
	public Result supDidConfirm(String confirmId) {//太坑了，这里传过来的其实是t_gen_protocol_unite.id
		return protocolConfirmService.supDidConfirm(confirmId);
	}

	/**
	 * 用于测试报价的页面
	 */
	@RequestMapping("/test")
	public String test() {
		return "/gtimpframe/protocol/test";
	}


}
